import axios from "axios";
import type { CreateAxiosDefaults } from "axios";

const config: CreateAxiosDefaults = {
  baseURL: "/api",
  timeout: 5000
};

export const request = axios.create(config);

request.interceptors.request.use(config => {
  const token = localStorage.getItem("token");
  if (token) {
    config.headers.Authorization = "Bearer " + token;
  }
  return config;
});

request.interceptors.response.use(
  response => {
    return response.data;
  },
  error => {
    if (error.response.status === 401) {
      window.location.href = "/login"; // Redirect to login page
    }
    return Promise.reject(error);
  }
);
